﻿using DBModels;
using Method.DAL;
using Method.DateTime;
using Models.VModels;
using RedisHelper.Article;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.WDS.Methods
{
    public class DisplayMet
    {
        /// <summary>
        /// 获取用户文章集合
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<V_ArticleList> GetUserArticles(int uid, int index)
        {
            List<ArticleList> list;
            using (ArticeContext context = new ArticeContext())
            {
                list = context.ArticleLists
                    .Where(u => u.ReleaseId == uid)
                    .Where(u => u.IsAllow == false)
                    .OrderByDescending(u => u.Time)
                    .ToList();
                if (list.Count > index * 10)
                {
                    var count = list.Count - (index * 10);
                    list.GetRange(index * 10, count > 10 ? 10 : count);
                }
            }

            List<V_ArticleList> retList = new List<V_ArticleList>();

            string avatar = null, name = null;
            if (!new GetUserInfo().GetAvatarAndNickNameByUserId(uid, ref avatar, ref name))
                return null;

            var like = new Like();
            list.ForEach(u =>
            {
                retList.Add(new V_ArticleList()
                {
                    UserId = uid,
                    UserName = name,
                    Avatar = avatar,
                    ArticleId = u.Id,
                    Time = GetPastTime.getTime(u.Time),
                    Title = u.Title,
                    Context = u.Text.Length < 100 ? u.Text.Substring(0, u.Text.Length) : u.Text.Substring(0, 100),
                    IsCurUser = true,
                    IsLike = like.IsArticleLike(u.Id, uid),
                    LikeCount = like.GetArticleLikeCount(u.Id),
                    CommentCount = new Comment().GetArticleCommentCount(u.Id)
                });
            });

            return retList;
        }
    }
}
